//
//  LzwBangumiDetailVC.m
//  CoJellyCartoon
//
//  Created by 刘志武 on 16/4/5.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "LzwBangumiDetailVC.h"
#import "LzwBangumiDetailHeaderView.h"
#import "LzwBangumiDetailFootterView.h"
#import "LzwBanguCollectCell.h"
#import "LzwBangDetailCollectCell.h"

#import "FinalPlayViewController.h"


@interface LzwBangumiDetailVC ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *myDetailCollectView;

@property (nonatomic, strong) LzwBangumiDetailHeaderView *myHeaderView;
@property (nonatomic, strong) LzwBangumiDetailFootterView *myFooterView;

@property (nonatomic, strong) UICollectionViewFlowLayout *myFlowLayout;


@property (nonatomic, strong) NSMutableArray *arrayTT;

@property (nonatomic, copy) NSString *stringPL;


@property (nonatomic, strong) NSMutableArray *arrayAV_id;


@end

@implementation LzwBangumiDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"番剧详情";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 20, 20);
    [button setImage:[[UIImage imageNamed:@"L20.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(LeftButtonAxtion:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = barItem;
    
    
    
    //初始化数组
    self.arrayUp = [NSMutableArray array];
    self.arrayPL = [NSMutableArray array];
    self.arrayTT = [NSMutableArray array];
    self.arrayAV_id = [NSMutableArray array];
    //创建collectview
    [self creatCollectView];
    
}

- (void)creatCollectView{
    
    self.myDetailCollectView = ({
    
        self.myFlowLayout = [[UICollectionViewFlowLayout alloc]init];
        
//        self.myFlowLayout.itemSize = CGSizeMake(80, 30);
        self.myFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        self.myFlowLayout.minimumInteritemSpacing = 3;
        self.myFlowLayout.minimumLineSpacing = 10;
//        self.myFlowLayout.sectionHeadersPinToVisibleBounds = YES;
        self.myFlowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 250);
//        self.myFlowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 240);
        
        UICollectionView *myCollectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:self.myFlowLayout];
        myCollectView.backgroundColor = [UIColor lightGrayColor];
        myCollectView.delegate = self;
        myCollectView.dataSource = self;
    
        [self.view addSubview:myCollectView];
        myCollectView;
    });
    
    
    [self.myDetailCollectView registerClass:[LzwBangDetailCollectCell class] forCellWithReuseIdentifier:@"cell"];
    
    //注册头部
    [self.myDetailCollectView registerClass:[LzwBangumiDetailHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    [self.myDetailCollectView registerClass:[LzwBanguCollectCell class] forCellWithReuseIdentifier:@"cellA"];
    
    
    [GiFHUD show];
    [GiFHUD setGifWithImageName:@"hold.gif"];
    
//    //请求数据
    [self getBangDetailData:_ting_ID];
//    [self getPLData:_oldID];
    

}

///设置每个分区item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return CGSizeMake(80, 30);
    }else{
        //高度自适应
        
        
        return CGSizeMake(SCREEN_WIDTH, 140);
    }
    
    
}

///设置每个分区的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
      
        return CGSizeMake(SCREEN_WIDTH, 250);
    }

    else{
        return CGSizeZero;

    }
}



-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
    
    
}



//头部和尾部
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    
   
    
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 0) {
    
        self.myHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
//
        return _myHeaderView;
    }
      
        
        
   }

    
    return nil;
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    if (section == 0) {
        return self.modelB.episodes.count;
    }else{
        return self.arrayTT.count;
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwBangDetailCollectCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    if (indexPath.section == 1) {
        
        LzwBanguCollectCell *cellD = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellA" forIndexPath:indexPath];

        if (_arrayPL.count != 0) {
            cellD.backgroundColor = [UIColor whiteColor];
        cellD.modelP = _arrayPL[indexPath.row];
            
         }
        return cellD;
    }
    
    cell.modelH = _modelB.episodes[indexPath.row];

    
    return cell;
}




- (void)getBangDetailData:(NSInteger)pageID{
    

    NSString *url = [NSString stringWithFormat:@"http://app.bilibili.com/bangumi/seasoninfo/%ld", pageID];
    
    
    //(@"nnnnnnnnn = %@", url);
    
    [WZAFNetTool GetNetWithURL:url WithBody:nil headfile:nil response:WZJSON success:^(id reuslt) {
        
        NSMutableDictionary *dict = [reuslt objectForKey:@"result"];
        
        self.modelB = [[LzwBangumiDetailModle alloc]init];
        
        [self.modelB setValuesForKeysWithDictionary:dict];
        
        [self.arrayUp addObject:self.modelB];
        
        self.myHeaderView.modelDetail = self.modelB;

        NSMutableArray *arrayW = [dict objectForKey:@"episodes"];
        
        
        for (NSMutableDictionary *dict1 in arrayW) {
            
            LzwFFFMdle *modelFF = [[LzwFFFMdle alloc]init];
            
            
            [modelFF setValuesForKeysWithDictionary:dict1];
            
            
            [self.arrayAV_id addObject:modelFF];
            
        }


        [self.arrayUp addObject:self.modelB];
        
        self.myHeaderView.modelDetail = self.modelB;
        
        [self getPLData:_modelF.av_id];
        
        [self.myDetailCollectView reloadData];
        
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}

///评论数据
- (void)getPLData:(NSString *)oldID{
    
    
    NSString *string = [NSString stringWithFormat:@"http://api.bilibili.com/x/reply?_device=android&_hwid=53c9b4a7dd017094&appkey=c1b107428d337928&build=414000&oid=%@&platform=android&pn=1&ps=20&sort=0&type=1&sign20=", oldID];

    //(@".... = %@", string);
    
    [WZAFNetTool GetNetWithURL:string WithBody:nil headfile:nil response:WZJSON success:^(id reuslt) {
        
        
        
        
        NSMutableDictionary *dict = [reuslt objectForKey:@"data"];
        
        
        
        
        self.arrayTT = [dict objectForKey:@"replies"];
        
        for (NSMutableDictionary *diction in _arrayTT) {
            
            LzwPLModel *modelP = [[LzwPLModel alloc]init];
            
            [modelP setValuesForKeysWithDictionary:diction];
            
            [self.arrayPL addObject:modelP];
            
        }
        
        [self.myDetailCollectView reloadData];
        [GiFHUD dismiss];

    } failure:^(NSError *error) {
        
        
        
        
    }];
    
    
}



//item的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FinalPlayViewController *finalVc = [[FinalPlayViewController alloc]init];
    
    LzwFFFMdle *modle = _arrayAV_id[indexPath.row];
    
    //(@"mmmmmm = %@", modle.av_id);
    
    finalVc.Aid = modle.av_id;
    
[self presentViewController:finalVc animated:YES completion:^{
    
    
}];
    
    
}


- (void)LeftButtonAxtion:(UIButton *)btn{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
